package com.yzt.message;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.zhuju.mvp.CommonPresenter;
import com.yzt.zhuju.mvp.MVPFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.message.CallRecordBean;
import com.yzt.zhuju.widget.refreshHeader.WaterDropHeader;
import com.yzt.message.adapter.CallRecordAdapter;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：bobwang
 * 创建时间：2023-09-06 21:38:13
 * 描述：
 */
public class CallRecordFragment extends MVPFragment<CommonPresenter> implements OnRefreshListener {

    private CallRecordAdapter callRecordAdapter;

    private RefreshLayout mRefreshLayout;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_call_record;
    }

    @Override
    public void installViews() {

        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRefreshLayout.setRefreshHeader(new WaterDropHeader(mActivity));
        mRefreshLayout.setOnRefreshListener(this);

        RecyclerView recycle = findViewById(R.id.blacklist_rv);
        recycle.setLayoutManager(new LinearLayoutManager(mActivity));
        callRecordAdapter = new CallRecordAdapter();
        recycle.setAdapter(callRecordAdapter);

//        callRecordAdapter.addChildClickViewIds(R.id.remove_tv);
//        callRecordAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
//            @Override
//            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
//                if (view.getId() == R.id.remove_tv) {
//                    BlackListBean entry = (BlackListBean) adapter.getItem(position);
//                    showMProgress();
//                    // 移除黑名单
//                    BizLayer.getInstance().getAccountModule().cancelBlack(entry.getUidRel(), 0, new AresSubscriber<HttpResponse>() {
//                        @Override
//                        protected void onSuccess(HttpResponse httpResponse) {
//                            callRecordAdapter.remove(position);
//                            if (callRecordAdapter.getData().size() == 0) {
//                                callRecordAdapter.setEmptyView(getEmptyView());
//                            }
//                        }
//
//                        @Override
//                        protected void onFinallyEnd() {
//                            super.onFinallyEnd();
//                            closeProgress();
//                        }
//                    });
//                }
//            }
//    });

        callRecordAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            mCurrentPage++;
            getHttpData();
        });

        List<CallRecordBean> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(new CallRecordBean());
        }


        callRecordAdapter.addData(list);

        //mRefreshLayout.autoRefresh();
    }

    /**
     * 获取黑名单列表
     */
    public void getHttpData() {
        BizLayer.getInstance().getAccountModule().blackList(mCurrentPage, PAGE_SIZE, new AresSubscriber<List<CallRecordBean>>() {
            @Override
            protected void onSuccess(List<CallRecordBean> list) {
                if (mCurrentPage == 1) {
                    if (list.size() == 0) {
                        callRecordAdapter.setEmptyView(getEmptyView());
                    } else {
                        callRecordAdapter.removeEmptyView();
                    }
                    callRecordAdapter.setList(list);
                } else {
                    callRecordAdapter.addData(list);
                }
                callRecordAdapter.getLoadMoreModule().loadMoreComplete();
                if (list.size() < PAGE_SIZE) {
                    callRecordAdapter.getLoadMoreModule().loadMoreEnd();
                }
            }

            @Override
            protected void onError(int code, String info) {
                super.onError(code, info);
                if (mCurrentPage == 1) {
                    callRecordAdapter.setEmptyView(getErrorView(info));
                }
            }

            @Override
            protected void onFinallyEnd() {
                super.onFinallyEnd();
                mRefreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mCurrentPage = 1;
        getHttpData();
    }

    @Override
    public void netWorkRequest() {

    }
}
